import 'package:flutter/material.dart';
import 'package:flutter_4/SQLiteHelper.dart';
import 'package:flutter_4/SharedPreferencesHelper.dart';
import 'package:flutter_4/model/UserInfo.dart';
import 'package:oktoast/oktoast.dart';

class ProfilePage extends StatefulWidget {
  @override
  _ProfilePageState createState() => _ProfilePageState();
}

class _ProfilePageState extends State<ProfilePage> {
  List menuTitles = [
    '我的消息',
    '阅读记录',
    '我的问答',
    '我的收藏',
    '个人信息'
  ];
  List menuIcons = [
    Icons.message,
    Icons.watch_later,
    Icons.phone,
    Icons.auto_awesome,
    Icons.account_circle
  ];

  TextEditingController passwordController = new TextEditingController();

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('我的'),
      ),
      body: ListView.separated(
          itemBuilder: (context, index) {
            if (index == 0) {
              return Container(
                color: Colors.indigo,
                height: 150.0,
                child: Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      GestureDetector(
                        child: Container(
                          width: 60.0,
                          height: 60.0,
                          decoration: BoxDecoration(
                              shape: BoxShape.circle,
                              border: Border.all(
                                color: Colors.white,
                                width: 2.0,
                              ),
                              image: DecorationImage(
                                image: AssetImage(
                                    '7.png'),
                                fit: BoxFit.cover,
                              )),
                        ),
                        onTap: () {
                          //TODO:执行登录
                        },
                      ),
                      SizedBox(
                        //加间距
                        height: 10.0,
                      ),
                      Text(
                        '点击添加头像',
                        style: TextStyle(color: Colors.white),
                      )
                    ],
                  ),
                ),
              );
            }
            index -= 1;
            return GestureDetector(
              onTap: (){
                menuOnTap(index, context);
              },
              child: ListTile(
                leading: Icon(menuIcons[index]), //左图标
                title: Text(menuTitles[index]), //中间标题
                trailing: Icon(Icons.arrow_forward_ios),
              ),
            );
          },
          separatorBuilder: (context, index) {
            return Divider();
          }, //分隔线
          itemCount: menuTitles.length + 1),
    );
  }

  SQLiteHelper _sqLiteHelper = new SQLiteHelper();
  /// 菜单点击事件
  menuOnTap(int index, BuildContext context) async {
    if(index == 4){
      String account = await SharedPreferencesHelper.get('account');
      String password = await SharedPreferencesHelper.get('password');
      showDialog(
        context: context,
        barrierDismissible: true, //点击弹窗外部是否消失
        builder: (BuildContext context) => Container(
          height: 400,
          child: AlertDialog(
            title: new Text(//标题
              '提示',
              style:
              new TextStyle(color: Colors.red[300], fontSize: 18),
            ),
            content: Align(
              child: Container(
                height: 400,
                child: Column(
                  children: [
                    Text('当前账号: $account'),
                    Text('当前密码: $password'),
                    Row(
                      children: [
                        Text('修改密码: '),
                        Expanded(
                            child: TextField(
                              //监听
                              controller: passwordController,
                              decoration: InputDecoration(
                                hintText: '请输入修改的密码',
                              ),
                            )
                        )
                      ],
                    )
                  ],
                ),
              ),
            ),//提示语
            actions: <Widget>[
              new FlatButton(//一个扁平的Material按钮
                  onPressed: () {
                    Navigator.of(context).pop();//弹窗消失
                  },
                  child: Text('取消')),
              new FlatButton(
                //对话框按钮
                  onPressed: () async {
                    String newPassword = passwordController.text;
                    if(newPassword.isEmpty ||
                        newPassword.length < 8 ||
                        newPassword.length > 15 ){
                      showToast('密码需要大于8位小于15位');
                    } else {
                      await _sqLiteHelper.openSqlite();
                      UserInfo info = await _sqLiteHelper.getUserInfoByAccount(account);
                      info.password = newPassword;
                      print('要修改的userInfo: ${info.toJson()}');
                      _sqLiteHelper.updateByAccount(info);

                      // _sqLiteHelper.close();

                      showToast('修改密码成功!');
                      SharedPreferencesHelper.save('password', newPassword);
                      passwordController.clear();
                      Navigator.pop(context);

                      outPutSqlite();
                    }
                  },
                  child: Text('修改密码')),
            ],
          ),
        ),
      );
    }
  }

  /// 输出更新之后的数据表
  outPutSqlite() async {
    await _sqLiteHelper.openSqlite();
    List<UserInfo> userInfoList = await _sqLiteHelper.queryAll();
    print('更新数据库个人信息表：');
    if(userInfoList!= null){
      for(int i=0; i< userInfoList.length; i++){
        print(userInfoList[i].toJson());
      }
    }
    await _sqLiteHelper.close();
  }

}
